$(function(){
	var $formEdit=$("#form_edit");
	var oldTagId = "";
	$.ajax({ // 此处用同步处理，避免出现val()比reload早的问题
		url : serverURL + "file/getInfo?fileId="+fileId,
		type : "get",
		async : false,
		success : function(data) {
			if (data.code == 0) {
				data = data.data;
				$("#introduction").val(data.introduction);
				$("[name=name]").val(data.name);
				$("[name=price]").val(data.price);
				$("[name=cardinal]").val(data.cardinal);
				$("#url").val(data.path);
				$("#fileSize").val(data.size);
				if(data.authStatus ==1){
					$("#file_path").html("<a href=http://101.37.82.219/upload/file"+data.path+" >您已经上传过文件，请点击下载后确认</a>");
				}
				var detail = data.list;
				var detailId = "";
				for (var i = 0; i < detail.length; i++) {
					detailId += detail[i].dcId + ",";
				}
				detailIdList = detailId.substring(0, detailId.length - 1).split(",");
				var tagList = data.tagList;
				var tag = "";
				var oldId = "";
				for (var i = 0; i < tagList.length; i++) {
					tag += tagList[i].name + ",";
					oldId += tagList[i].tagId + ",";
				}
				oldTagId = oldId.substring(0, oldId.length - 1).split(",");
				$("#tags").val(tag);
				var tcId = data.tcId;
				var scId = data.scId;
				var typeId = data.typeId;
				editPlan(data.hierId);
				editType(typeId);
				editCategory(tcId, scId, detailIdList);
				if(data.grade == 1){
					$("#form_edit").find("[name=grade]").html("<option value='1' selected='selected'>入门</option><option value='2' >进阶</option>");
				}else if(data.grade == 2){
					$("#form_edit").find("[name=grade]").html("<option value='1' >入门</option><option value='2' selected='selected'>进阶</option>");					
				}
				$("#tags").tagsInput({
					width : 'auto',
					onChange : function(elem, elem_tags) {
					},
				});

			} else if (data.code != -1) {
				alert("数据添加失败");
			}
		}
	});
    /** ************增加文件*************** */
	var $formAdd=$("#form_edit");
    
	function checkAddForm(){
		if($("[name=name]").val() == ""){
			warnInput($("[name=name]"));
			return false;
		}
		if($("[name=type]").val() == 0){
			warnInput($("[name=type]"));
			return false;
		}
		if($("[name=top]").val() == 0){
			warnInput($("[name=top]"));
			return false;
		}
		if($("[name=second]").val() == 0){
			warnInput($("[name=second]"));
			return false;
		}
//		if($("[name=detail]").val() == 0){
//			warnInput($("[name=detail]"));
//			return false;
//		}
		if($("[name=cardinal]").val() == ""){
			warnInput($("[name=cardinal]"));
			return false;
		}
		return true;
	}
	
	 $("#btn_confirm_edit").click(function() {
		 if($("#url").val() == null || $("#url").val() == ""){
			 alert("请先上传文件");
			 return false;
		 }
		if (checkAddForm()) {
			name = $("[name=name]").val();
			path = $("#url").val();
			typeId =$formAdd.find($("[name=type]")).val();
			tcId =$formAdd.find($("[name=top]")).val();
			scId =$formAdd.find($("[name=second]")).val();
			mold = $formAdd.find("[name=mold]").val();
			size = $("#fileSize").val();
			hierId = $formAdd.find($("[name=hierId]")).val();
			grade = $formAdd.find($("[name=grade]")).val();
			
			dcIds = $formAdd.find($("[name=detail]")).val();
			if(mold == 0){//工程资料有四级分类
				if($formAdd.find($("[name=content]")).val() != 0){
					dcIds += ","+$formAdd.find($("[name=content]")).val() ;
				}
				
				if($formAdd.find($("[name=project_mode]")).val() != 0){
					dcIds += ","+ $formAdd.find($("[name=project_mode]")).val();
				}
			}

			var price = 0;
			if($formAdd.find($("[name=price]")).val() != ""){
				price = $formAdd.find($("[name=price]")).val();
			}
			introduction = $("textarea[name='content1']").val();
			
			var tag = $("#tags").val();
			$.ajax({ // 此处用同步处理，避免出现val()比reload早的问题
				url : serverURL + "tag/updateBatch?tags="+tag+"&oldTagId="+oldTagId,
				type : "post",
				async : false,
				success : function(data) {
					if (data.code == 0) {
						$.post(serverURL + "file/modify", {"fileId":fileId,"authStatus":'1',"mold":mold,"grade":grade,"hierId":hierId,"dcIds":dcIds,"name" : name,"path" : path,"size" : size,"tagIds" : data.data,"typeId" : typeId,"tcId" : tcId,"scId" : scId,"introduction":introduction,"price":price}, function(data) {
							if (data.code == 0) {
									alert("修改成功");		
									window.location.href="file.htm";
								} else if (data.code != -1) {
									alert("修改失败,请稍后重试");
								}
						});
	
					} else if (data.code != -1) {
						alert("数据添加失败");
					}
				}
			});					
		}
	}) 
	
	
	/***********添加文件类型****************/
	function editType(type){
		$.get("type/list", function(data) {
		   $("#form_edit").find("[name=type]").empty();
			$.each(data.data, function(i, obj) {
					var option = "";
					if(type == obj.typeId){
						option +="<option value='" + obj.typeId + "' selected='selected'>" + obj.typeName+ "</option>";
					}else{
						option +="<option value='" + obj.typeId + "'>" + obj.typeName+ "</option>";
					}
					$("#form_edit").find("[name=type]").append(option);
			});
		}, "json");
	}

	function editPlan(planId){
		$.get("view/plan/getList",{"all":true}, function(data) {
			$("#form_edit").find("[name=hierId]").empty().html("<option value='0' selected='selected'>请选择规划体系...</option>");
			$.each(data.data.list,function(i,k){
				var option = "";
				if( k.hierId == planId){
					option +="<option value='" + k.hierId + "' selected>" + k.hierName+ "</option>";
				}else{
					option +="<option value='" + k.hierId + "'>" + k.hierName+ "</option>";
				}
				$("#form_edit").find("[name=hierId]").append(option);
			});
		}, "json");
	} 
});